package com.anjian.bamin.anjiandianzimap.widget;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.anjian.bamin.anjiandianzimap.R;

/**
 * 分级
 */
public class ChooseDialog extends Dialog {
    private Context context;
    private RadioGroup mRadioGroup;
    private int chooseMode ;
    private int[] radioButtons = { R.id.radio_one, R.id.radio_two, R.id.radio_three,R.id.radio_all};
    private boolean isAll;
    private OnBtnClickListener onBtnClickListener;



    public ChooseDialog(Context context,int chooseMode,boolean isAll) {
        super(context, R.style.dialog);
        this.context = context;
        this.isAll = isAll;
        this.chooseMode = chooseMode;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.dialog_choose, null);
        setContentView(view);
        Button mBtnSure = (Button) view.findViewById(R.id.btn_sure);
        mRadioGroup = (RadioGroup) view.findViewById(R.id.radio_group);
        RadioButton mRadioAll = (RadioButton) view.findViewById(R.id.radio_all);
        RadioButton mRadioOne = (RadioButton) view.findViewById(R.id.radio_one);
        RadioButton mRadioTwo = (RadioButton) view.findViewById(R.id.radio_two);
        RadioButton mRadioThree = (RadioButton) view.findViewById(R.id.radio_three);
        if(!isAll){
            mRadioAll.setVisibility(View.GONE);
            mRadioAll.setChecked(false);
            mRadioOne.setChecked(true);
        }else{
            mRadioAll.setVisibility(View.VISIBLE);
            mRadioAll.setChecked(true);
            mRadioOne.setChecked(false);

        }

        switch (chooseMode) {
            case 0:
                mRadioOne.setChecked(true);
                break;
            case 1:
                mRadioTwo.setChecked(true);

                break;
            case 2:
                mRadioThree.setChecked(true);

                break;
            case 3:
                mRadioAll.setChecked(true);
                break;

        }


        mBtnSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int checkedRadioButtonId = mRadioGroup.getCheckedRadioButtonId();
                for (int i = 0; i < radioButtons.length; i++) {
                    if (radioButtons[i] == checkedRadioButtonId) chooseMode = i;
                }

                if (onBtnClickListener != null) {
                    onBtnClickListener.onCompanyClick(chooseMode);
                }
                dismiss();
            }
        });

        Window dialogWindow = getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        DisplayMetrics d = context.getResources().getDisplayMetrics(); // 获取屏幕宽、高用
        lp.width = (int) (d.widthPixels * 0.6); // 高度设置为屏幕的0.6
        dialogWindow.setAttributes(lp);
    }
    public void setOnBtnClickListener(OnBtnClickListener listener) {
        this.onBtnClickListener = listener;
    }
    public interface OnBtnClickListener {
        void onCompanyClick(int companyInfo);
    }
}